/***********************************************************************
** Copyright (C) 2011 Anthony Naddeo <anthony.naddeo@gmail.com>
**
** This file is part of qtweather-qml
**
** qtweather-qml is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 3 of the License, or
** (at your option) any later version.
**
** qtweather-qml is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this package; If not, see <http://www.gnu.org/licenses/>.
**
***********************************************************************/

import QtQuick 1.1
import com.meego 1.0
import "components"

Sheet {
	id: root
	acceptButtonText: "Done"

	signal selected(string zipcode)
	signal update(string zipcode)
	property alias animateRunning: toggle.checked

	content: Item{
		width: parent.width
		height: parent.height

		SwitchSection{id: toggle
			text: "Enable animations"
			checked: Utilities.home("animate") !== "null" ? (parseInt(Utilities.home("animate")) ? true : false) : true
			onCheckedChanged: Utilities.setHome("animate",  (checked ? 1 : 0)  )
		}


		Flickable {
			anchors.fill: parent
			anchors.topMargin: toggle.height

			contentWidth: col.width
			contentHeight: col.height
			flickableDirection: Flickable.VerticalFlick
			clip: true

			Column {
				id: col
				anchors.top: parent.top
				width: parent.width
				spacing: 10

				Repeater{
					id: repeater
					model: 5
					signal check(int ind)
					Item{
						width: root.width
						height: button.height
						Button{
							id: button;
							text: Utilities.home(index+1) !== "null" ? (Utilities.home(Utilities.home(index+1)) !== "null" ? Utilities.home(Utilities.home(index+1)) : Utilities.home(index+1)) : "Home " + (index+1)
							width: 200
							anchors.left: parent.left
							onClicked: {root.selected(textField.text); root.accept()}
							Connections{
								target: root
								onUpdate: button.text = Utilities.home(index+1) !== "" ? (Utilities.home(Utilities.home(index+1)) !== "" ? Utilities.home(Utilities.home(index+1)) : Utilities.home(index+1)) : "Home " + (index+1)
							}

						}
						TextField{
							id: textField
							validator: RegExpValidator{regExp: /[0-9][0-9][0-9][0-9][0-9]/}
							placeholderText: "zipcode";
							text: Utilities.home(index+1)
							anchors.left:  button.right;
							anchors.right: parent.right;
							anchors.rightMargin: checkbox.width
							anchors.margins: 5
							onTextChanged: Utilities.setHome((index+1),text)
							inputMethodHints: Qt.ImhDigitsOnly | Qt.ImhNoPredictiveText

						}
						RadioButton{
							id: checkbox
							anchors.right: parent.right
							anchors.verticalCenter: parent.verticalCenter
							checked: (index+1) === Utilities.defaultHome() ? true : false
							onClicked: {
								if(checkbox.checked)
									Utilities.setDefaultHome(index+1)
								else
									Utilities.setDefaultHome(0)
								repeater.check(index)

							}
							Connections{
								target: repeater
								onCheck:{
									if(ind !== index)
										checkbox.checked = false
								}
							}
						}
					}
				}




			}

		}
	}

}
